<template>
  <div>
    <h2>添加书籍</h2>
    书名<input type="text" v-model="name">
    价格<input type="number" v-model="price">
    描述<input type="text" v-model="desc">
    图片<input type="file" id="img">
    <button @click="sub">添加</button>
  </div>
</template>

<script>
export default {
  data(){
    return{
      name:'',
      price:0,
      desc:'',

    }
  },
  mounted(){
    this.uid=sessionStorage.getItem('uid');
    if(!this.uid){
      this.$router.push({
        name:'Login'
      });
      return false;
    }

  },
  methods:{
    sub:function(){
      //
      let img=document.getElementById('img').files[0];

      let formdata=new FormData();
      formdata.append('name',this.name);
      formdata.append('desc',this.desc);
      formdata.append('price',this.price);
      formdata.append('uid',this.uid);
      formdata.append('img',img);

      this.axios({
        url:'http://127.0.0.1:8000/app01/books/',
        methods:'post',
        data:formdata
      }).then(res=>{
        if(res.data.code==1000){
          this.$router.push({
            name:'List'
          })
        }
      })
    }
  }
}
</script>

<style>

</style>
